package com.example.springboot_code_read.typefilter;

import com.example.springboot_code_read.annotation.MyAnnotation;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class TypeFilterTest {

    public static void main(String[] args) throws IOException {

        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

        SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();

        TypeFilter typeFilter = new AnnotationTypeFilter(MyAnnotation.class);

        Resource[] resources = resourcePatternResolver.getResources("classpath*:com/example/springboot_code_read/**/*.class");

        for (Resource resource : resources) {

            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);

            boolean match = typeFilter.match(metadataReader, metadataReaderFactory);

            System.out.println("扫描到的文件: " + resource.getFilename() + " 是否匹配: " + match);

        }


    }

}
